__author__ = 'toriycn'

import subprocess
import sys

def monitor(frist_invoke = 1):
    shell_command = "free"
    result = subprocess.check_output(shell_command,shell=True)
    result = str(result.decode("utf-8"))
    #获取的是内存使用了多少？

    value_dic = {}
    total,used,free,cacheused,cachefree = [None]*5
    res = result.split('\n')

    for i in range(len(res)):
        if i == 1:
          total,used,free = res[i].split()[1:4]
          cachefree = res[i].split()[6]
        if i == 2 :
          cacheused= res[i].split()[2]

    value_dic = {
        'tottal':total,
        'used':used,
        'free':free,
        'cacheused':cacheused,
        'cachefree':cachefree,
        'useage':int((int(free)+int(cachefree)-int(cacheused))/int(total)*100),
        'status':0
    }

    return value_dic


if __name__ == '__main__':
    print(monitor())